/*
 *
 *  Copyright (C) 2009 LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Routes;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.LibreGeoSocial.App.R;


public class RouteList extends ListActivity {

	private static final String TAG 		= "TraceList";
	private static final String TAG_ERROR 	= TAG + "::ERROR";

	// Context menu
	private static final int MENU_DELETE_ROUTE = Menu.FIRST + 1;
	
	TracesAdapter mAdapter; 
	
	public RouteManager trManager = null;
	private ArrayList<String> lTrace = null;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		Intent i = getIntent();
        
        Boolean storage  = i.getBooleanExtra("storage", Boolean.TRUE);
        
		RouteMap.currentTracefile = null;

		if (trManager == null)
			trManager = new RouteManager(getBaseContext());
		
		trManager.setStorageSDCard(storage);
		lTrace = trManager.getRoutesName();
		
  		// Adapter for ListView
        mAdapter = new TracesAdapter(this);
        mAdapter.setTraceManager(trManager);
        setListAdapter(mAdapter);
        
        registerForContextMenu(this.getListView());
		
	}
	

	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,  ContextMenuInfo menuInfo)
	{ 
          menu.add (0, MENU_DELETE_ROUTE, 0, "Delete Route");		
	}
	
	
	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		String traceName = trManager.getRoutesName().get(info.position);
		
		
		switch (item.getItemId()) {
		
		case MENU_DELETE_ROUTE:
			
			if (trManager.deleteRoute(traceName))
			{
				Toast.makeText (getBaseContext(), 
	                    		"Route deleted correctly!", 
	                    		Toast.LENGTH_LONG).show();
			}
			else
			{
				Toast.makeText (getBaseContext(), 
                				"Error! The route is not deleted.", 
                				Toast.LENGTH_LONG).show();
			}
			
			mAdapter.notifyDataSetChanged();
			
			break;
				
		}
	
		return true;
		
	}
	
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        
        String trace = trManager.getRoutesName().get(position);
        RouteMap.currentTracefile = trace;
        try {
        	
			this.finish();
			
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			Log.e("RouteList",e.getMessage());
		}
    
	}

	public static class TracesAdapter extends BaseAdapter {

		private Context mContext;
		private RouteManager mtrManager = null; 
		
		public TracesAdapter(Context c) {
            mContext = c;
        }
		
		public void setTraceManager (RouteManager trManager)
		{
			mtrManager = trManager;
		}
		
		public int getCount() {
			// TODO Auto-generated method stub
			return mtrManager.getRoutesName().size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.generic_list, null);
	        } else {
	            // Use convertView if it is available
	            view = convertView; 
	        }
				        
	        
	        String trace = mtrManager.getRoutesName().get(position);
	        
	        trace = (trace.split("_")[1]);
			
	        ImageView img = (ImageView) view.findViewById(R.id.image);	        
			img.setImageResource(R.drawable.ruta_blanco_48);
			
			TextView t = (TextView) view.findViewById(R.id.title);
			t.setText(trace);
			
			t = (TextView) view.findViewById(R.id.owner);
			t.setText(trace);
	        
			return view; 	
	        
	        
		}

	}



}
